<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="90px"
      >
        <el-form-item label="工厂" prop="factory">
          <el-select
            v-model="queryParams.factory"
            placeholder="请选择工厂"
            style="width: 240px"
            clearable
            filterable
            @change="handleSearchListChange('factory')"
          >
            <el-option
              v-for="(item, index) in factoryOptions"
              :key="index"
              :label="item.deptName"
              :value="item.deptCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工段" prop="workshopCode">
          <el-select
            v-model="queryParams.workshopCode"
            placeholder="请选择工段"
            clearable
            filterable
            style="width: 240px"
            @change="handleSearchListChange('workSection')"
          >
            <el-option
              v-for="(item, index) in workshopOptions"
              :key="index"
              :label="item.deptName"
              :value="item.deptCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产线" prop="lineCode">
          <el-select
            v-model="queryParams.lineCode"
            placeholder="请选择产线"
            style="width: 240px"
            clearable
            filterable
            @change="handleSearchListChange('productLine')"
          >
            <el-option
              v-for="(item, index) in lineOptions"
              :key="index"
              :label="item.deptName"
              :value="item.deptCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工艺段" prop="sectionCode">
          <el-select
            v-model="queryParams.sectionCode"
            placeholder="请选择工艺段"
            style="width: 240px"
            clearable
            filterable
            @change="handleSearchListChange('sectionCode')"
          >
            <el-option
              v-for="(item, index) in processOptions"
              :key="index"
              :label="item.deptName"
              :value="item.deptCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单编号" prop="orderCode">
          <el-input
            v-model="queryParams.orderCode"
            placeholder="请输入订单编号"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="prodName">
          <el-input
            v-model="queryParams.prodName"
            placeholder="请输入产品名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="投料批号" prop="feedingBatchNo">
          <el-input
            v-model="queryParams.feedingBatchNo"
            placeholder="请输入投料批号"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="时间段" prop="date">
          <el-date-picker
            v-model="queryParams.date"
            style="width: 240px"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          ></el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            filterable
            style="width: 240px"
          >
            <el-option
              v-for="(item, index) in change_shifts_status"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button type="primary" icon="Edit" @click="handleSuccession"
          v-hasPermi="['produce:feeding:add']"
            >投料</el-button
          >
        </el-form-item>
      </el-form>
  
      <el-table
        v-loading="loading"
        :data="typeList"
        border
        style="width:100%"
        max-height="500px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column width="100px" label="生产日期" prop="prodDate" />
        <el-table-column width="100px" label="班次" prop="classesName" />
        <el-table-column
          label="班组"
          prop="workGroupName"
          :show-overflow-tooltip="true"
          width="140"
        >
          <!-- <template #default="scope">
            <el-link
              type="primary"
              v-if="scope.row.status == 1"
              @click="handleDetailing(scope.row)"
              >{{ scope.row.workGroupName }}</el-link
            >
            <el-link
              type="primary"
              v-else-if="scope.row.status == 2"
              @click="handleDetailhas(scope.row)"
              >{{ scope.row.workGroupName }}</el-link
            >
          </template> -->
        </el-table-column>
        <el-table-column width="100px" label="订单编号" prop="orderCode" />
        <el-table-column width="100px" label="生产产品" prop="prodName" />
        <el-table-column width="100px" label="投料内容" prop="matName" />

        <!-- <el-table-column width="100px" label="投料批号" prop="feedingBatchNo" /> -->

        <el-table-column
        label="投料批号"
        prop="feedingBatchNo"
        :show-overflow-tooltip="true"
        width="140"
      >
        <template #default="scope">
          <el-link v-hasPermi="['produce:feeding:query']"  type="primary" @click="goDetail(scope.row)">{{
            scope.row.feedingBatchNo
          }}</el-link>
        </template>
      </el-table-column>
        <el-table-column width="100px" label="投入数量" prop="inputQty" />
        <el-table-column width="60px" label="单位" prop="inputUnit" />
        <el-table-column width="170px" label="投料时间" prop="feedingTime" />
        <el-table-column width="100px" label="操作人" prop="operator" />
        <el-table-column width="170px" label="记录时间" prop="recordTime" />
       
        <el-table-column  width="180px" label="工厂" prop="factoryName" />
        <el-table-column  width="130px" label="工段" prop="workshopName" />
        <el-table-column  width="130px" label="产线" prop="lineName" />
        <el-table-column  width="130px" label="工艺段" prop="sectionName" />
        <el-table-column
          label="操作"
          align="center"
          width="80"
          fixed="right"
          class-name="small-padding fixed-width"
        >
        <!-- v-if="scope.row.status == 1" -->
          <template #default="scope" >
            <el-button
              link
              type="primary"
              v-if="scope.row.status == 1"
              icon="Edit"
               v-hasPermi="['produce:feeding:edit']"
              @click="handleUpdate(scope.row)"
              
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </template>
  
  <script setup name="changeShifts">
  import { listFeeding } from "@/api/productionMgt/feeding";
  import { getFactoryTree, depeIdSelectList } from "@/api/system/dept";
  import DataInfo from "./data";
  const { proxy } = getCurrentInstance();
  
  const typeList = ref([]);
  const { change_shifts_status } = proxy.useDict("change_shifts_status");
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const dateRange = ref([]);
  var factoryOptions = ref([]);
  var workshopOptions = ref([]);
  var lineOptions = ref([]);
  var processOptions = ref([]);
  
  const data = reactive({
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      name: undefined,
    },
  });
  
  const { queryParams, form } = toRefs(data);
  /** 查询设备类型列表 */
  function getList() {
    loading.value = true;
    let newForm = JSON.parse(JSON.stringify(queryParams.value));
    if (queryParams.value?.date?.length) {
      newForm.feedingBeginTime = queryParams.value.date[0] + " 00:00:00";
      newForm.feedingEndTime = queryParams.value.date[1] + " 23:59:59";
      delete newForm.date;
    }
    listFeeding(proxy.addDateRange(newForm)).then((response) => {
      typeList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  function getFactorys() {
    // getFactoryTree(query).then((res) => {
    //   factoryOptions.value = res.data;
    // });
    depeIdSelectList({
      deptType: "factory",
      deptCode: '',
      permissionType:'permission'
    }).then((res) => {
      factoryOptions.value = res.data;
    });
  }
  
  function handleSearchListChange(type) {
    if (type == "factory") {
      depeIdSelectList({
        deptType: "station",
        deptCode: queryParams.value.factory,
        permissionType:'permission'
      }).then((res) => {
        workshopOptions.value = res.data;
        lineOptions.value = [];
        processOptions.value = [];
        queryParams.value.workshopCode = "";
        queryParams.value.lineCode = "";
        queryParams.value.sectionCode = "";
      });
    }
    if (type == "workSection") {
      depeIdSelectList({
        deptType: "line",
        deptCode: queryParams.value.workshopCode,
        permissionType:'permission'
      }).then((res) => {
        lineOptions.value = res.data;
        processOptions.value = [];
        queryParams.value.lineCode = "";
        queryParams.value.sectionCode = "";
      });
    }
    if (type == "productLine") {
      depeIdSelectList({
        deptType: "craft",
        deptCode: queryParams.value.lineCode,
        permissionType:'permission'
      }).then((res) => {
        processOptions.value = res.data;
        queryParams.value.sectionCode = "";
      });
    }
  }
  
  function handleDetailing(row) {
    proxy.subPage(SuccessionData, {
      title: "接班详情",
      data: { ...row, parentType: "view" },
    });
  }
  
  // function handleDetailhas(row) {
  //   proxy.subPage(HandoverData, {
  //     title: "交接班详情",
  //     data: { ...row, parentType: "view" },
  //   });
  // }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    dateRange.value = [];
    proxy.resetForm("queryRef");
    queryParams.value.status = '';
    handleQuery();
  }
  
  function handleSuccession() {
    proxy.subPage(
      DataInfo,
      { title: "投料", data: { parentType: "add" } },
      () => {
        getList();
      }
    );
  }
  
  /** 修改按钮操作 */
  function handleUpdate(row) {
    if (!proxy.checkPermi(['produce:feeding:query'])) {
      return
    }
    proxy.subPage(
      DataInfo,
      { title: "编辑投料", data: { ...row, parentType: "update" } },
      () => {
        getList();
      }
    );
  }
  /** 修改按钮操作 */
  function goDetail(row) {
    proxy.subPage(
      DataInfo,
      { title: "投料详情", data: { ...row, parentType: "view" } },
      () => {
        getList();
      }
    );
  }
  
  
  /** 多选框选中数据 */
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  getList();
  getFactorys();
  onMounted(async () => {
    getList();
  });
  </script>
  